package com.c201801020208.ybl.Company.Activity;

import androidx.appcompat.app.AppCompatActivity;

import androidx.fragment.app.Fragment;

import androidx.viewpager.widget.ViewPager;

import android.graphics.drawable.Drawable;

import android.os.Bundle;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import com.c201801020208.ybl.Adapters.MainFragmentPageAdapter;

import com.c201801020208.ybl.Company.HomePageFragment;
import com.c201801020208.ybl.Company.MyCompanyFragment;
import com.c201801020208.ybl.Company.ResumeScreeningFragment;
import com.c201801020208.ybl.R;

import com.c201801020208.ybl.model.impl.ChatFragment;

import java.util.ArrayList;

import java.util.List;

public class Main2Activity extends AppCompatActivity {

    private ViewPager main_viewPager;
    private List<Fragment> fragments = new ArrayList<>();
    private MainFragmentPageAdapter adapter;
    private RadioGroup rgp;
    public static String username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        username = getIntent().getStringExtra("username");
        initView();
        initData();
        setAdapter();
        setListener();
        initRadioButton();
    }

    public static String getUsername() {
        return username;
    }

    //设置RadioButton的大小
    private void initRadioButton() {
        RadioButton radioButton1 = findViewById(R.id.radio1);
        Drawable homepageDrawable = getResources().getDrawable(R.drawable.menu1_drawable_selector);
        homepageDrawable.setBounds(0, 0, 69, 69);//参数1是距左右边距离，参数2是距上下边距离，参数3长度,参数4宽度
        radioButton1.setCompoundDrawables(null, homepageDrawable, null, null);//只放上面

        RadioButton radioButton2 = findViewById(R.id.radio2);
        Drawable homepageDrawable2 = getResources().getDrawable(R.drawable.menu2_drawable_selector);
        homepageDrawable2.setBounds(0, 0, 69, 69);//参数1是距左右边距离，参数2是距上下边距离，参数3长度,参数4宽度
        radioButton2.setCompoundDrawables(null, homepageDrawable2, null, null);//只放上面

        RadioButton radioButton3 = findViewById(R.id.radio3);
        Drawable homepageDrawable3 = getResources().getDrawable(R.drawable.menu3_drawable_selector);
        homepageDrawable3.setBounds(0, 0, 69, 69);//参数1是距左右边距离，参数2是距上下边距离，参数3长度,参数4宽度
        radioButton3.setCompoundDrawables(null, homepageDrawable3, null, null);//只放上面

        RadioButton radioButton4 = findViewById(R.id.radio4);
        Drawable homepageDrawable4 = getResources().getDrawable(R.drawable.menu4_drawable_selector);
        homepageDrawable4.setBounds(0, 0, 69, 69);//参数1是距左右边距离，参数2是距上下边距离，参数3长度,参数4宽度
        radioButton4.setCompoundDrawables(null, homepageDrawable4, null, null);//只放上面

    }

    //初始化控件
    private void initView() {
        main_viewPager = (ViewPager) findViewById(R.id.main_viewpager);
        main_viewPager.setOffscreenPageLimit(4);
    }

    //初始化数据
    private void initData() {
        HomePageFragment homePageFragment = new HomePageFragment();
        MyCompanyFragment myCompanyFragment = new MyCompanyFragment();
        ResumeScreeningFragment resumeScreeningFragment = new ResumeScreeningFragment();
        ChatFragment chatFragment = new ChatFragment();
        fragments.add(resumeScreeningFragment);
        fragments.add(chatFragment);
        fragments.add(homePageFragment);
        fragments.add(myCompanyFragment);
    }

    //设置适配器
    private void setAdapter() {
        //实例化适配器
        adapter = new MainFragmentPageAdapter(getSupportFragmentManager(), fragments);
        //设置适配器
        main_viewPager.setAdapter(adapter);
        rgp = (RadioGroup) findViewById(R.id.main_rgp);

        RadioButton rb = (RadioButton) rgp.getChildAt(0);
        rb.setChecked(true);

    }

    private void setListener() {
        //viewPager的滑动监听
        main_viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                //获取当前位置的RadioButton
                RadioButton rb = (RadioButton) rgp.getChildAt(position);
                //设置为true
                rb.setChecked(true);
            }
        });

        //RadioGroup的事件监听
        rgp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for (int index = 0; index < rgp.getChildCount(); index++) {
                    RadioButton rb = (RadioButton) rgp.getChildAt(index);
                    if (rb.isChecked()) {
                        main_viewPager.setCurrentItem(index, false);
                        break;
                    }
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        int id = getIntent().getIntExtra("id", 3);
        if (id == 1) {
            main_viewPager.setCurrentItem(3);
        }
    }

}
